package com.tscnd.core.security;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.tscnd.core.exception.TSException;
import com.tscnd.core.service.user.IUserService;
import com.tscnd.core.utils.SysContextUtil;

public class MyLogoutFilter implements Filter {

	private String filterProcessesUrl;

	@Autowired
	private IUserService userService;

	// ~ Constructors
	// ===================================================================================================

	// ~ Methods
	// ========================================================================================================

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException
	{
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		SysContextUtil.setRequest(request);
		SysContextUtil.setResponse(response);
		String path = request.getRequestURI();
		if (path.contains(filterProcessesUrl))
		{
			System.out.println("用户注销后发生...");
			try
			{
				userService.LogOut();
			} catch (TSException e)
			{
				e.printStackTrace();
			}

		}
		chain.doFilter(request, response);
	}

	@Deprecated
	public void setFilterProcessesUrl(String filterProcessesUrl)
	{
		this.filterProcessesUrl = filterProcessesUrl;
	}

	@Deprecated
	protected String getFilterProcessesUrl()
	{
		return filterProcessesUrl;
	}

	@Override
	public void destroy()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException
	{
		// TODO Auto-generated method stub

	}
}
